<?php

/**
 * @author      :   nghialh
 * @name        :   Fpt_BBAuthenticate
 * @version     :   201103
 * @copyright   :   My company
 * @todo        :   Using to authenticate
 */
class My_BBAuthenticate implements Zend_Auth_Adapter_Interface {

    /**
     * Performs an authentication attempt
     *
     * @throws Zend_Auth_Adapter_Exception If authentication cannot
     *                                     be performed
     * @return Zend_Auth_Result
     */
    public function authenticate() {
        $user = array();
        $identity = array(
            'user_id' => null,
            'fpt_id' => null,
            'username' => null,
            'full_name' => null,
            'avatar' => null,
        );

        /** @var My_Business_Service_FospApi */
        $instanceServiceFospApi = ServiceFospApi::getInstance();
        $user = $instanceServiceFospApi->getUserInfo();

        if (empty($user)) {
            return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $identity);
        } else {
            $user = current($user);
            if (empty($user['email'])) {
                //FAILURE_IDENTITY_AMBIGUOUS
                return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS, $identity);
            }

            if ($user['userid'] > 0) {
                $instanceUser = User::getInstance();
                $arrUser = $instanceUser->getListLimit(array('fpt_id' => $user['userid']), 1, 1);

                //Startup user
                if (empty($arrUser)) {
                    list($day, $month, $year) = explode("-", $user['birthday'], 3);
                    $arrData = array(
                        'fpt_id' => $user['userid'],
                        'country_id' => 196,
                        'city_id' => 0,
                        'district_id' => 0,
                        'ward_id' => 0,
                        'username' => $user['email'],
                        'full_name' => $user['fullname'],
                        'gender' => $user['gender'],
                        'birth_date' => "{$year}-{$month}-{$day}",
                    );

                    $userID = $instanceUser->insert($arrData);
                    //Redirect logout page
                    if (!$userID) {
                        
                    }
                } else {
                    $arrUser = current($arrUser);
                    $userID = $arrUser['user_id'];
                    // force edit user info
                    if ($arrUser['username'] != $user['email']) {
                        $instanceUser->update($arrUser['user_id'],array('username' => $user['email']));
                    }
                }

                $instanceGroupUser = GroupUser::getInstance();
                $GroupUser = $instanceGroupUser->getDetailByID($userID);

                $identity = array(
                    'user_id' => $userID,
                    'fpt_id' => $user['userid'],
                    'group_id' => (int) $GroupUser['group_id'],
                    'username' => $user['email'],
                    'full_name' => $user['fullname'],
                    'avatar' => $user['avatar'],
                );

                return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
            } else {
                return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $identity);
            }
        }
    }

}